package com.efounder.frame.activity.helper;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import androidx.core.content.res.ResourcesCompat;
import android.widget.LinearLayout;

import com.core.xml.StubObject;
import com.efounder.frame.tabbar.EFTabBar;
import com.efounder.frame.tabbar.EFTabBarItem;
import com.efounder.frame.utils.EFAppAccountUtils;
import com.efounder.frame.utils.EFDrawableUtils;
import com.efounder.frame.xmlparse.EFAppAccountRegistry;
import com.efounder.frame.xmlparse.EFXmlConstants;
import com.efounder.mobilemanager.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XinQing on 2016/8/18.
 * EFAppAccountMainActivity 辅助类
 */
public class EFAppAccountMainActivityHelper {

    /**
     * 创建底部 EFTabBar
     * @param activity
     * @param dataList
     * @return
     */
    public EFTabBar createTabBar(Activity activity, List<StubObject> dataList) {
        LinearLayout tabbarLayout = (LinearLayout) activity.findViewById(R.id.tabbarLayout);
        // 1.初始化TabBar
        List<EFTabBarItem> EFTabBarItems = createTabBarItems(activity,dataList);
        EFTabBar efTabBar = new EFTabBar(activity, EFTabBarItems);
        // tabBar.setPadding(0, 20, 0, 20);
        // tabBar.setBackgroundColor(0xFFff0000);
        // tabBar.setSelectedTab(1);
        efTabBar.setTabSelectedColor(ResourcesCompat.getColor(activity.getResources(),R.color.chat_red,null));
        efTabBar.setTabColor(ResourcesCompat.getColor(activity.getResources(),R.color.ef_gray_dark,null));
        efTabBar.setTabPadding(20);
        efTabBar.setTabTextSize(13);
        efTabBar.setTabPadding(0);
        efTabBar.setTabBackgroundColor(activity.getResources().getColor(R.color.ef_white));
//        // 2.TabBar添加OnTabClickListener
//        efTabBar.setOnTabBarItemClickListener(this);
//        //设置选中的TabBarItem
//        efTabBar.setSelectedTab(currentPosition);
        tabbarLayout.addView(efTabBar);

        return efTabBar;
    }

    /**
     * 创建底部TabbarItem
     *
     * @return
     */
    private List<EFTabBarItem> createTabBarItems(Activity activity, List<StubObject> data) {
        List<EFTabBarItem> EFTabBarItems = new ArrayList<EFTabBarItem>();
        for (StubObject stubObject: data ) {
            String caption = stubObject.getString(EFXmlConstants.ATTR_CAPTION,"");

            EFTabBarItem item = new EFTabBarItem(activity, caption,getTabItemDrawable(activity,stubObject));
            EFTabBarItems.add(item);
        }
        return EFTabBarItems;
    }

    /**
     * 从sdCard中读取图片，如果没有，默认返回白班
     * @param activity
     * @param stubObject
     * @return
     */
    private Drawable getTabItemDrawable(Activity activity, StubObject stubObject){
        String normalIconName = stubObject.getString(EFXmlConstants.ATTR_NORMAL_ICON, "");
        Drawable drawable = EFDrawableUtils.getDrawableFromConfigurationFile(EFAppAccountUtils.getAppAccountImagePath(),normalIconName);
        if (drawable == null){
            drawable = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_launcher,null);
        }
        return drawable;
    }

    /**
     * 初始化注册表
     */
    public void initRegistry() {
        EFAppAccountRegistry.init(EFAppAccountUtils.getAppAccountAppFramePath());
    }

}
